﻿using System;
using System.Collections.Generic;

namespace Wrox.ProCSharp.Generics
{
  public class DocumentManager<T>
      where T : IDocument//给泛型TDocument添加接口IDocument的约束
    {
    private readonly Queue<T> documentQueue = new Queue<T>();

    public void AddDocument(T doc)
    {
      lock (this)
      {
        documentQueue.Enqueue(doc);
      }
    }

    public bool IsDocumentAvailable => documentQueue.Count > 0;

        //如果TDocument没有实现IDocument接口，这个类型强制转换会导致一个运行时异常
        //给泛型TDocument添加接口IDocument的约束
    public void DisplayAllDocuments()
    {
      foreach (T doc in documentQueue)
      {
        Console.WriteLine(doc.Title);
      }
    }

        //用default指定引用类型默认值null和值类型0
    public T GetDocument()
    {
      T doc = default(T);
      lock (this)
      {
        doc = documentQueue.Dequeue();
      }
      return doc;
    }
  }
}
